
var Login = {
    changeInput:function () {
        $('.tel,#sms_code,#account,#pwd').bind('input propertychange', function(e) {
            Login.checkInput();
        });
    },
    checkInput:function () {
        var id = $('.login_box.block').attr('id'),
            pattern = Common.patternTel;
        if(id == 'sms_login'){
            var tel = parseInt($('.tel').val());
            var code = $.trim($('#sms_code').val());
            if(pattern.test(tel) && code.length > 3){
                $('.sign_in_but').addClass('enable');
            }else{
                $('.sign_in_but').removeClass('enable');
            }
        }else {
            var account = parseInt($('#account').val());
            var pwd = $.trim($('#pwd').val());
            if(pattern.test(account) && pwd.length >7 && pwd.length < 20){
                $('.sign_in_but').addClass('enable');
            }else {
                $('.sign_in_but').removeClass('enable');
            }
        }

    },
    switch:function(){
        var a = $('.switch_bar a');
        $.each(a, function (index, value) {
            $(this).on('touchend',function(){
                $('.switch_bar a').removeClass('active');
                $(this).addClass('active');
                $('.contain_box .login_box').removeClass('block').eq(index).addClass('block');
                $('.sign_in_but').removeClass('enable');
                if(index > 0){
                    $('.tips').addClass('none');
                    $('.reg_2').removeClass('none');
                }else {
                    $('.reg_2').addClass('none');
                    $('.tips').removeClass('none');
                }

            });

        });
    },
    getCode:function () {
        $('.get_sms_code').on('touchend',function(e){
            e.preventDefault();
            var tel = $(this).closest('li').find('.tel'),
                user = parseInt(tel.val()),
                pattern = Common.patternTel;
            if($(this).hasClass('readonly')){
                return false;
            }
            if(!pattern.test(user))
            {
                show_hint("手机号输入有误!");
                tel.focus();
                return false;
            }else {
                Login.sendSms(user);

            }

        });
    },

    sendSms:function (user){
        if(user==''){
            return false;
        }
        var csrfToken = $.trim($('meta[name=csrf-token]').attr('content'));
        $.ajax({
            type:"POST",
            url:"/mem/get_sms_code",
            data:{tel:$.trim(user),_csrf: $.trim(csrfToken),t:Math.random()},
            dataType: "json",
            success:function(data){
                if(data.status == 0){
                    show_hint(data.msg);
                    var count = 90;
                    var set = setInterval(timer, 1000);
                    function timer() {
                        count--;
                        $('.get_sms_code').html('重新发送('+count+')').addClass('readonly');
                        if (count == 0) {
                            $('.get_sms_code').html('发送验证码').removeClass('readonly');
                            clearInterval(set);
                        }
                    }
                    return true;
                }else {
                    show_hint(data.msg);
                }


            },error: function(){
                show_hint("发送失败,请稍后重试");
                return false;
            }
        });
    },
    login:function () {
        $(".sign_in_but").on("touchend",function(e)
        {
            e.preventDefault();
            var pattern = Common.patternTel;
            var csrfToken = $.trim($('meta[name=csrf-token]').attr('content'));
            var idName  = $.trim($('.login_box.block').attr('id'));
            if(idName == 'sms_login'){
                var tel = parseInt($('#sms_login .tel').val()),
                    code = $.trim($('#sms_code').val());
                if(pattern.test(tel) && code.length > 3){
                    $.post("/mem/check_sms_code", {tel: tel,code:code, _csrf: csrfToken, t: Math.random()}, function (data) {
                        if (data.status != 0) {
                            show_hint("验证码输入有误");
                            return false;
                        } else {
                            Login.commit('/mem/quick',tel,code,csrfToken);
                            return true;
                        }
                    }, "json");
                }else {
                    show_hint('手机号或验证码格式不正确!');
                    return false;
                }

            }else {
                var account = parseInt($('#pwd_login #account').val()),
                    pwd = $.trim($('#pwd_login #pwd').val());
                if(pattern.test(account) && pwd.length > 6){
                    $('#_csrf').val(csrfToken);
                    $('#pwd_login').submit();
                    return true;
                }else {
                    show_hint('手机号或密码格式不正确!');
                    return false;
                }
            }


        });
    },
    commit:function (url,user,guard,csrfToken) {
        $.ajax({
            type:"POST",
            url:url,
            data:{users: user,userPwd:guard, _csrf: csrfToken, t: Math.random()},
            dataType: "json",
            success:function(data){
                if(data.error != 0){
                    show_hint(data.msg);
                }else {
                    if(data.data.flag > 0){
                        show_hint(data.msg+',您的初始密码为sb+您登录手机号后六位数字');
                    }else{
                        show_hint(data.msg);
                    }
                    var time=setInterval (showTime, 100),second = 1;
                    function showTime() {
                        if(second === 0){
                            window.location=data.data.url;
                            clearInterval(time);
                        }
                        if(second < 0){
                            second = 0;
                        }
                        second--;

                    }
                }

            }
        });

    }
};

var Profile = {
    switchChooseSex:function () {
        var isClickc = true;
        $('.radio').on('touchstart', function(e){
            isClickc = true;
        });
        $('.radio').on('touchmove', function(e){
            isClickc = false;
        });
        $('.radio').on('touchend', function(e){
            if (isClickc == true){
                $('.radio').find('span').removeClass('after');
                $('.radio').find('span').find('input').prop('checked',false);
                $(this).find('span').first().addClass('after');
                $(this).find('span').find('input').prop('checked',true);
            }
        });
    },
    address:function () {
        var area1 = new LArea();
        area1.init({
            'trigger': '#address_name', //触发选择控件的文本框，同时选择完毕后name属性输出到该位置
            'valueTo': '#address_value', //选择完毕后id属性输出到该位置
            'keys': {
                id: 'id',
                name: 'name'
            }, //绑定数据源相关字段 id对应valueTo的value属性输出 name对应trigger的value属性输出
            'type': 1, //数据源类型
            'data': LAreaData //数据源
        });
        area1.value=[1,13,3];//控制初始位置，注意：该方法并不会影响到input的value
    },
    
    saveInfo:function () {
        $(".editUserInfo #submit").on("touchend",function(e){
            var pwdPattern = /^[0-9a-zA-Z_]{8,20}$/;
            var emailPattern =/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
            var username = $.trim($('#edit_form input[name=usercontact]').val()),
               sex = parseInt($('#edit_form input[name=usersex]:checked').val()),
               addr = $.trim($('#edit_form input[name=useraddr]').val()),
               company = $.trim($('#edit_form input[name=usercompany]').val()),
               qq = $.trim($('#edit_form input[name=userqq]').val()),
               email = $.trim($('#edit_form input[name=useremail]').val()),
               wx = $.trim($('#edit_form input[name=weixin]').val()),
               pwd = $.trim($('#edit_form input[name=userpass]').val());


           if(username.length > 0 || sex > 0 || addr.length > 0 || company.length > 0 || qq.length > 0 || email.length > 0 || wx.length > 0 || pwd.length > 0){
                if(pwd.length > 0){
                    if(!pwdPattern.test(pwd)){
                        show_hint('密码为字母数字下划线,8-20个字符!');
                        return false;
                    }
                }

                if(email.length > 0){
                    if(!emailPattern.test(email)){
                        show_hint('邮箱格式不正确!');
                        return false;
                    }
                }
               if(username.length > 0){
                    if(username.length > 20){
                        show_hint('姓名仅允许在20个字符以内!');
                        return false;
                    }

               }
               if(company.length > 0){
                   if(company.length > 50){
                       show_hint('公司名称仅允许在50个字符以内!');
                       return false;
                   }

               }
               if(wx.length > 0){
                   if(wx.length > 20){
                       show_hint('微信号仅允许在20个字符以内!');
                       return false;
                   }

               }

               var _hmt = _hmt || [];
               var inputs = $('#edit_form input');

               $.each(inputs,function (k,v) {
                   var name = $(v).attr('name');
                   if(name != '_csrf' && name != 'usermob'){
                       if(name == 'usersex'){
                           var sex = parseInt($('input[name=usersex]:checked').val());
                           if(!isNaN(sex)){
                               _hmt.push(['_trackEvent', 'newjjwap', '编辑个人信息'+name, sex]);
                           }
                       }else{
                           if($(v).val() !=''){
                               _hmt.push(['_trackEvent', 'newjjwap', '编辑个人信息'+name, $.trim($(v).val())]);

                           }
                       }

                   }


               });
               var data = $('#edit_form').serialize();
               $.ajax({
                   type:"POST",
                   url:'/mem/editmemberinfo',
                   data:data,
                   dataType: "json",
                   success:function(data){
                       if(data.status == 0){
                           show_hint(data.msg);
                           location.href = '/mem/index';
                       }else {
                           show_hint('数据未做更改或编辑信息失败!');
                       }

                   },error: function(){
                       show_hint("网络繁忙,请稍后重试");
                       return false;
                   }
               });

           }else {
               return false;
           }


        });
    }
};


